package gui;

import java.io.IOException;
import java.net.UnknownHostException;

import netGaming.NetGame;

/**
 * @author Wojciech Łopata
 *
 */

public class NetGameSettingsPanel extends javax.swing.JPanel
{
	private static final long serialVersionUID = 3904650755390653201L;
	private final MainFrame mainFrame;
	
	/** Creates new form NetGameSettingsPanel */
	public NetGameSettingsPanel(MainFrame mainFrame)
	{
		this.mainFrame = mainFrame;
		hostAddressLabel = new javax.swing.JLabel("Adres serwera:");
		portLabel = new javax.swing.JLabel("Port:");
		hostAddressField = new javax.swing.JTextField();
		portField = new gui.PortField();
		joinGameButton = new javax.swing.JButton("Dołącz do gry");
		initComponents();
	}

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    
    private void initComponents() {
        joinGameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(hostAddressLabel)
                            .addComponent(portLabel))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(portField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(hostAddressField, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(joinGameButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(hostAddressLabel)
                    .addComponent(hostAddressField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(portLabel)
                    .addComponent(portField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(joinGameButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
    {
    	NetGame netGame = null;
    	try
		{
			netGame = new NetGame(hostAddressField.getText(), portField.getPort(),
					mainFrame.getBoardDrawer().getMovementSource());
		} catch (UnknownHostException e)
		{
			mainFrame.showMessage("Nie znaleziono serwera");
			return;
		} catch (IOException e)
		{
			mainFrame.showMessage("Nie udało się nawiązać połączenia z serwerem");
			return;
		}
    	mainFrame.setGameInfo(netGame);
    	mainFrame.startDrawing();
    }


    // Variables declaration - do not modify
    private javax.swing.JButton joinGameButton;
    private javax.swing.JLabel hostAddressLabel;
    private javax.swing.JLabel portLabel;
    private javax.swing.JTextField hostAddressField;
    private gui.PortField portField;
    // End of variables declaration

}
